//
//  AppDelegate.swift
//  ALSinaWeiBoSwift
//
//  Created by 柴瑞军 on 16/4/8.
//  Copyright © 2016年 myCompany. All rights reserved.
//

import UIKit

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?


    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
        // Override point for customization after application launch.
        let account = ALUserAccountViewModel.userAccountViewModel.userAccount
        print("账户信息:\(account)")
        
        let barButton = UIBarButtonItem.appearance()
        barButton.tintColor = UIColor.orangeColor()
//        创建窗口
       
        window = UIWindow(frame:  UIScreen.mainScreen().bounds)
        
        
//        let tab = ALMainTabBarController()
        
//        设置根控制器
//        window?.rootViewController = tab
        window?.rootViewController = defaultViewController()
//        让窗口成为主窗口并显示
        
        window?.makeKeyAndVisible()
        
//        打印最新的版本
//        ALPrint("是否是最新版本:\(isNewVersion())")
        return true
    }

    
//    判断是否是最新版本
    private func isNewVersion() -> Bool {
    
//    获取当前的版本号
        let currentVersion = NSBundle.mainBundle().infoDictionary?["CFBundleShortVersionString"] as! String
//    获取上一次的版本号
        let sandboxkey = "sandboxKey"
        let sandboxVersion = NSUserDefaults.standardUserDefaults().stringForKey(sandboxkey)
//        对比
        let newVersion = currentVersion != sandboxVersion
//        是最新版本,保存新的版本号
        if newVersion {
            NSUserDefaults.standardUserDefaults().setObject(currentVersion, forKey: sandboxkey)
//            立即同步
            NSUserDefaults.standardUserDefaults().synchronize()
        }
        return newVersion
    }
    
//    进入程序,设置界面的根控制器
    
    private func defaultViewController() -> UIViewController{
    
        if !ALUserAccountViewModel.userAccountViewModel.userLogin {
//            没有登录
//            ALPrint("\(ALUserAccountViewModel.userAccountViewModel.userAccount)")
            return ALMainTabBarController()
        }
        
//        标识已经登录,判断是不是新版本
        let version = isNewVersion()
        return version ? ALNewFeatureController() : ALWelcomeController()
    }
    
    
//    切换控制器的方法.统一来管理切换控制器的切换
    func switchViewController(controller:UIViewController){
     window?.rootViewController = controller
    }
    
    func applicationWillResignActive(application: UIApplication) {
        // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
        // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
    }

    func applicationDidEnterBackground(application: UIApplication) {
        // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
        // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
    }

    func applicationWillEnterForeground(application: UIApplication) {
        // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
    }

    func applicationDidBecomeActive(application: UIApplication) {
        // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
    }

    func applicationWillTerminate(application: UIApplication) {
        // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
    }


}

